       <template>
    <div>
        <div class="one">
            <el-form ref="form" :model="form" label-width="80px" v-if="show">
                <el-form-item label="仓库名称" class="size" prop="name">
                    <el-input v-model="form.name" placeholder="请输入名称"></el-input>
                </el-form-item>
                <el-form-item label="仓库地址" class="size" prop="address">
                    <el-input v-model="form.address" placeholder="请输入名称"></el-input>
                </el-form-item>
                <el-form-item class="item">
                    <el-button type="primary" icon="el-icon-search" @click="getData()">搜索</el-button>

                    <el-button icon="el-icon-refresh-left" @click="Reset()">重置</el-button>
                </el-form-item>
            </el-form>
            <div class="Newexportsearchrefresh">
                <div class="Addexport">
                    <el-button type="primary" plain icon="el-icon-plus" @click="addLists()">新增</el-button>
                    <el-button type="warning" plain icon="el-icon-download" class="yello-button" @click="exportss()">导出</el-button>
                </div>
                <div class="Searchrefresh">
                    <el-tooltip placement="top">
                        <div slot="content">{{ disabled ? '显示搜索' : '隐藏搜索' }}</div>
                        <el-button icon="el-icon-search" circle @click="Showorhide()"> </el-button>
                    </el-tooltip>
                    <el-tooltip content="刷新" placement="top">
                        <el-button icon="el-icon-refresh-left" circle @click="Reset()"></el-button>
                    </el-tooltip>
                </div>
            </div>
        </div>
        <el-table :data="tableData"  style="width: 100%">
            <el-table-column label="序号" width="130" type="index" align="center" :index="indexMethod"> </el-table-column>
            <el-table-column prop="name" label="仓库名称" width="180" align="center"> </el-table-column>
            <el-table-column prop="address" label="仓库地址" width="180" align="center"> </el-table-column>
            <el-table-column prop="code" label="仓库条码" width="190" align="center"> </el-table-column>
            <el-table-column prop="contacts" label="联系人" width="180" align="center"> </el-table-column>
            <el-table-column prop="phone" label="手机号码" width="190" align="center"> </el-table-column>
            <el-table-column prop="category" label="类别" width="180" align="center"> </el-table-column>
            <el-table-column fixed="right" label="操作" width="200" align="center">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="update(scope.row)">修改</el-button>
                    <el-button type="text" size="small" @click="delet(scope.row.wId)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="block">
            <el-pagination
            style="float:right;"
            background
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="form.pageNum"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="form.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
            >
            </el-pagination>
        </div>
        <!-- 修改和添加弹窗 -->
        <el-dialog :title="Dialogtitle" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="仓库名称" prop="name">
                    <el-input v-model="ruleForm.name"></el-input>
                </el-form-item>
                <el-form-item label="地址" prop="address">
                    <el-input v-model="ruleForm.address"></el-input>
                </el-form-item>
                <el-form-item label="条码" prop="code">
                    <el-input v-model="ruleForm.code"></el-input>
                </el-form-item>
                <el-form-item label="联系人" prop="contacts">
                    <el-input v-model="ruleForm.contacts"></el-input>
                </el-form-item>
                <el-form-item label="手机号码" prop="phone">
                    <el-input v-model="ruleForm.phone"></el-input>
                </el-form-item>
                <el-form-item label="类别" prop="category">
                    <el-input v-model="ruleForm.category"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
                    <el-button @click="dialogVisible = false">取 消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script>
import { Warehousedata, changeStatus, jurisdiction, Deletewarehouse,exporte,exporte2 } from '@/api/user';
import { downloadXlsx } from "../../utils/xlsx.js";
import Icon from './Icon.vue';
export default {
    components: { Icon },
    data() {
        return {
            // 新增表单验证
            rules: {
                name: [
                    { required: true, message: '请输入仓库名称', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                address: [
                    { required: true, message: '请选择活动区域', trigger: 'change' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                code: [
                    { required: true, message: '请选择条码', trigger: 'change' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                contacts: [
                    { required: true, message: '请填写联系人', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                phone: [
                    { required: true, message: '请填写手机号码', trigger: 'blur' },
                    { min: 11, max: 11, message: '长度11的字符', trigger: 'blur' }
                ],
                category: [
                    { required: true, message: '请填写类别', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ]
            },
            // 弹窗标题
            Dialogtitle: '',
            // 弹窗
            ruleForm: {
                name: '',
                address: '',
                code: '',
                contacts: '',
                phone: '',
                category: ''
            },
            // 新增和修改
            conVar: '',
            // 总条目数
            total: 100,
            // 搜索
            form: {
                name: '',
                address: '',
                pageNum: 1,
                pageSize: 10
            },
            // 数据
            tableData: [],
            disabled: false,
            show: true,
            dialogVisible: false,
            f:{
                fileName:undefined,
                delete:true
            }
        };
    },
    methods: {
        // 删除
        delet(wId) {
            console.log(wId);
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    Deletewarehouse(wId).then((ref) => {
                        if (ref.code == 200) {
                            this.getData();
                            this.$message({
                                type: 'success',
                                message: ref.msg
                            });
                        } else {
                            this.$message({
                                type: 'success',
                                message: ref.msg
                            });
                        }
                    });
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        // 新增和修改
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (this.conVar == 0) {
                        changeStatus(this.ruleForm).then((ref) => {
                            this.getData();
                            this.dialogVisible = false;
                            if (ref.code == 200) {
                                this.$message({
                                    message: ref.msg,
                                    type: 'success'
                                });
                            } else {
                                this.$message.error(ref.msg);
                            }
                        });
                    } else {
                        jurisdiction(this.ruleForm).then((ref) => {
                            this.getData();
                            this.dialogVisible = false;
                            if (ref.code == 200) {
                                this.$message({
                                    message: ref.msg,
                                    type: 'success'
                                });
                            } else {
                                this.$message.error(ref.msg);
                            }
                        });
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });

            // else{

            // }
        },
        // 重置弹窗里的内容
        resect() {
            (this.ruleForm.name = undefined),
                (this.ruleForm.address = undefined),
                (this.ruleForm.code = undefined),
                (this.ruleForm.contacts = undefined),
                (this.ruleForm.phone = undefined),
                (this.ruleForm.category = undefined);
        },
        // 弹窗确认关闭
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then((_) => {
                    done();
                })
                .catch((_) => {});
        },
        // 新增
        addLists() {
            this.resect();
            this.dialogVisible = true;
            this.Dialogtitle = '添加仓库';
            this.conVar = 0;
        },
        // 修改
        update(e) {
            console.log(e);
            this.ruleForm = e;
            this.Dialogtitle = '修改仓库';
            this.conVar = 1;
            this.dialogVisible = true;
        },
        // 刷新和重置
        Reset() {
            this.$refs.form.resetFields();
            this.getData();
        },
        // 搜索框显示隐藏
        Showorhide() {
            (this.show = !this.show), (this.disabled = !this.disabled);
        },
        // 分页（多少个）
        handleSizeChange(val) {
            this.form.pageSize = Number(val);
            this.getData();
        },
        // 分页（页数）
        handleCurrentChange(val) {
            this.form.pageNum = Number(val);
            this.getData();
        },
        // 搜索数据
        getData() {
            Warehousedata(this.form).then((ref) => {
  
                this.tableData = ref.rows;
                this.total = ref.total;
            });
        },
        // 序号
        indexMethod(index) {
            return (this.form.pageNum - 1) * this.form.pageSize + index + 1;
        },
        // 导出
        exportss(){
            exporte().then((ref)=>{
                this.f.fileName = ref.msg
                exporte2(this.f).then((ref)=>{
                    // console.log(ref);
                    this.downloadXlsx(downloadXlsx(ref))
                })
            })
        }
    },
    mounted() {
        this.getData();
    }
};
</script>
<style scoped lang="less">
.size {
    font-weight: 550;
}
.one .el-form-item--small {
    width: 20%;
    display: inline-block;
}
.item /deep/ .el-form-item__content:last-child {
    margin-left: 20px !important;
}
.item /deep/ .el-button {
    height: 30px !important;
}
.Addexport /deep/ .el-button {
    widows: 70px !important;
}
/deep/ .yello-button {
    color: #ffba00;
    background: #fff8e6;
    border-color: #ffe399;
}
.Newexportsearchrefresh {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
}
/deep/ .el-table th.el-table__cell.is-leaf {
    border: unset !important;
}
/deep/.el-dialog {
    width: 35% !important;
}
.el-dialog__body /deep/.el-form-item--small {
    width: 80% !important;
}
.block{
    height: 30px;
    margin: 10px 0px;
}
</style>